package com.mall.product.controller;

import com.mall.api.dto.OrderDetailDTO;
import com.mall.common.entity.Result;
import com.mall.common.exception.GlobalException;
import com.mall.product.entity.dto.ProductQueryDTO;
import com.mall.product.entity.vo.ProductDetailVO;
import com.mall.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/product")
@Tag(name = "商品管理")
public class ProductController {

    private final ProductService productService;

    @GetMapping("/list")
    @Operation(summary = "商品列表 - 分页查询")
    public Result<?> getProductList(ProductQueryDTO queryDTO) {
        try {
            var pageResult = productService.getProductPage(queryDTO);
            return Result.success(pageResult);
        } catch (Exception e) {
            return Result.error("查询商品列表失败");
        }
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "根据id查询商品详情")
    public Result<ProductDetailVO> getProductDetail(@PathVariable("id") Long productId) {
        ProductDetailVO detail = productService.getProductDetail(productId);
        if (detail == null){
            return Result.error("商品不存在");
        }
        return Result.success(detail);
    }

    @GetMapping("/detail")
    @Operation(summary = "根据id批量查询商品")
    public Result<List<ProductDetailVO>> getProductDetailBatch(@RequestParam("ids") List<Long> ids) {
        return Result.success(productService.getProductDetailBatch(ids));
    }

    @PostMapping("/status")
    @Operation(summary = "商品上下架")
    public Result<?> changeProductStatus(@RequestParam("productId") Long productId,
                                           @RequestParam("status") Integer status) {
        try {
            boolean result = productService.changeProductStatus(productId, status);
            if (result) {
                String msg = status == 1 ? "商品上架成功" : "商品下架成功";
                return Result.success(msg);
            } else {
                return Result.error("操作失败");
            }
        } catch (Exception e) {
            if (e instanceof GlobalException) {
                return Result.error(e.getMessage());
            }
            return Result.error("操作失败");
        }
    }
    @DeleteMapping("{id}")
    @Operation(summary = "根据id删除商品")
    public void deleteItemById(@PathVariable("id") Long id){
        productService.removeById(id);
    }
    @PostMapping("/deduct-stock")
    @Operation(description = "批量扣减库存")
    public Result<String> deductStock(@RequestBody List<OrderDetailDTO> items) {
        try {
            productService.deductStock(items);
            return Result.success("库存扣减成功");
        } catch (GlobalException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(e.toString());
        }
    }
}